import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

//自定义模块
import '../module//date_format.dart';


class MsgUserItem extends StatelessWidget {
  Map userItem;
  bool tapStatus;
  MsgUserItem({Key key, @required this.userItem, this.tapStatus}):super(key:key);
  @override
  Widget build(BuildContext context) {
    var bgColor;
    if (this.tapStatus) {
      bgColor = Color.fromRGBO(227, 231, 234, 1);
    } else {
      bgColor = Colors.white;
    }
    return Container(
      width: ScreenUtil().setWidth(750),
      height: ScreenUtil().setWidth(130),
      color:bgColor,
      padding: EdgeInsets.all(ScreenUtil().setWidth(20)),
      child: Row(
        children: <Widget>[
          CircleAvatar(
            backgroundImage: NetworkImage(
              this.userItem['headimgurl'] == null ? 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1547632735106&di=a9311dc70751876de625c0b87fa5b890&imgtype=0&src=http%3A%2F%2Fpic2.zhimg.com%2Feb437e8e0e8ec7289f3a48bf67637271_b.jpg' : this.userItem['headimgurl']
            )
          ),
      Expanded(
        child: Container(
          margin: EdgeInsets.only(
            left: ScreenUtil().setWidth(20)
          ),
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      height: ScreenUtil().setWidth(30),
                      margin: EdgeInsets.fromLTRB(0, ScreenUtil().setWidth(7), 0, ScreenUtil().setWidth(15)),
                      child: Text(
                        this.userItem['nickname'] == null ? '没有名字' : this.userItem['nickname'],
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          fontSize: ScreenUtil().setWidth(30),
                          color: Color.fromRGBO(1, 1, 1, 1)
                        ),
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(
                      top: ScreenUtil().setWidth(8)
                    ),
                    child: Text(
                      this.userItem['last_butt_time'] == null ? '0' : this.userItem['last_butt_time'],
                      // 'hahah',
                      style: TextStyle(
                        color:Color.fromRGBO(142, 142, 142, 1),
                        fontSize: ScreenUtil().setWidth(22)
                      ),
                    ),
                  )
                ],
              ),
              Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      height: ScreenUtil().setWidth(30),
                      child: Text(
                        this.userItem['last_butt_msg'] == null ? '':this.userItem['last_butt_msg'],
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color:Color.fromRGBO(149, 149, 149, 1),
                          fontSize:  ScreenUtil().setWidth(24)
                        ),
                      ),
                    ),
                  ),
                  Badge(
                    num: this.userItem['new_num'] == null ? 0 : this.userItem['new_num']
                  )
                ],
              )
            ],
          ),
        ),
      )
        ],
      )
    );
  }
}

class Badge extends StatelessWidget {
  int num;
  Badge({Key key, @required this.num}):super(key:key);
  @override
  Widget build(BuildContext context) {
    var width;
    var text = '$num';
    if(this.num < 10 && this.num > 0){
      width = ScreenUtil().setWidth(36);
    } else if (this.num > 10 && this.num < 100) {
      width = ScreenUtil().setWidth(50);
    } else if (this.num >= 100) {
      width = ScreenUtil().setWidth(60);
      text = '99+';
    } else {
      return Container();
    }
    return Container(
      width: width,
      height: ScreenUtil().setWidth(36),
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: Color.fromRGBO(255, 93, 87, 1),
        borderRadius: BorderRadius.circular(16.0)
      ),
      child: Text(
        text,
        style: TextStyle(
          fontSize: ScreenUtil().setWidth(24),
          color: Colors.white
        ),
      ),
    );
  }
}